﻿using System;
using System.Windows.Forms;
using md.imi.membrane.common.Messaging;
using md.imi.membranes.objects;

namespace md.imi.membranes.gui.Dialogs
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmMessageBox : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public enum MessageTypes
        {
            msgInfo,
            msgError,
            msgWarning,
            msgQuestion
        } ;

        /// <summary>
        ///     Defaul contructo
        /// </summary>
        public FrmMessageBox()
        {
            InitializeComponent();
        }

        /// <summary>
        ///     Create a message box form with title, text and a type icon
        /// </summary>
        /// <param name="msgType"></param>
        /// <param name="title"></param>
        /// <param name="messgage"></param>
        public FrmMessageBox(MessageTypes msgType, String title, String messgage ) : this()
        {
            SetMessageIcon(msgType);
            Text = title;
            tbMessage.Text = messgage;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msgType"></param>
        /// <param name="title"></param>
        /// <param name="messgage"></param>
        /// <param name="ex"></param>
        public FrmMessageBox(MessageTypes msgType, String title, String messgage, Exception ex )
            : this()
        {
            SetMessageIcon(msgType);
            Text = title;
            tbMessage.Text = messgage;
            tbDetails.Text = ex.Message;

            if (!String.IsNullOrWhiteSpace(ex.Source))
                tbDetails.Text = String.Format("\nSource: {0}", ex.Source);

            if (ex.InnerException != null)
                tbDetails.Text  +=  Environment.NewLine + ex.InnerException.Message;
        }

        /// <summary>
        /// 
        /// </summary>
        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        /// <summary>
        /// Set the message box icon
        /// </summary>
        /// <param name="msgType"></param>
        private void SetMessageIcon(MessageTypes msgType)
        {
            switch(msgType)
            {
                case MessageTypes.msgInfo:
                    lblIcon.Text = "[ I ]";
                    pbDialogIcon.Image = Properties.Resources.icnInfo;
                    break;

                case MessageTypes.msgError:
                    lblIcon.Text = "[ E ]";
                    pbDialogIcon.Image = Properties.Resources.icnError;
                    break;

                case MessageTypes.msgQuestion:
                    lblIcon.Text = "[ Q ]";
                    pbDialogIcon.Image = Properties.Resources.icnQuestion;
                    break;

                case MessageTypes.msgWarning:
                    lblIcon.Text = "[ W ]";
                    pbDialogIcon.Image = Properties.Resources.icnWarning;
                    break;

                default:
                    lblIcon.Text = "[ I ]";
                    pbDialogIcon.Image = Properties.Resources.icnInfo;
                    break;

            }
        }

        /// <summary>
        ///     Show message as info
        /// </summary>
        /// <param name="message"></param>
        internal static void ShowInfo(string message)
        {
            WorkSpace.Instance.MessagesManager.AddMessage(new MessageData(null,
                DestinationType.LogsTarget, LogEntry.Info(null, message)));
            new FrmMessageBox(MessageTypes.msgInfo, "Info", message).ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public static void ShowError(string message, Exception ex)
        {
            WorkSpace.Instance.MessagesManager.AddMessage(new MessageData(null,
                DestinationType.LogsTarget, LogEntry.Error("{0} - {1}", message,
                ex != null ? ex.Message : "")));
            new FrmMessageBox(MessageTypes.msgError, "Error", message, ex).ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public static void ShowError(string message)
        {
            WorkSpace.Instance.MessagesManager.AddMessage(new MessageData(null,
                DestinationType.LogsTarget, LogEntry.Error(message)));
            new FrmMessageBox(MessageTypes.msgError, "Error", message).ShowDialog();
        }

    }
}
